package trade;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Utils.ScreenUtil;
import Utils.SpannableUtils;
import Utils.StringUtil;
import dao.MerchantTradeItem;

/**
 * by BF00233 on 2018/12/21.
 */
public class MerchantTradeAdapter extends DelegateAdapter.Adapter<MerchantTradeAdapter.MonthVH> {

    private final Context mContext;
    private List<MerchantTradeItem> mData;

    public MerchantTradeAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setupData(List<MerchantTradeItem> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        GridLayoutHelper layoutHelper = new GridLayoutHelper(1);
        layoutHelper.setVGap(ScreenUtil.dip2px(mContext, 1));
        return layoutHelper;
    }

    @NonNull
    @Override
    public MonthVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MonthVH(LayoutInflater.from(mContext).inflate(R.layout.item_merchant_trade, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MonthVH holder, int position) {
        if (position < 0 || position >= mData.size()) {
            return;
        }
        MerchantTradeItem merchantItem = mData.get(position);
        holder.tradeAmount.setText("交易金额：" + StringUtil.getFormatPrice(merchantItem.getTradeAmount()) + " 元");
        holder.tradeTime.setText("交易时间：" + StringUtil.getFormatDate(merchantItem.getBusinessTime()));

        SpannableStringBuilder sb1 = SpannableUtils.getBuilder("分润收益：")
                .append(StringUtil.getFormatPrice(merchantItem.getDividedAmount()) + " 元")
                .setForegroundColorId(R.color.red_fe3333)
                .create();

        holder.divideAmount.setText(sb1);
        SpannableStringBuilder sb2 = SpannableUtils.getBuilder("交易类型：")
                .append(merchantItem.getTradeType())
                .setForegroundColorId(R.color.blue_0ca6f0)
                .create();
        holder.tradeType.setText(sb2);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class MonthVH extends RecyclerView.ViewHolder {
        private TextView tradeAmount;
        private TextView tradeTime;
        private TextView divideAmount;
        private TextView tradeType;

        MonthVH(@NonNull View itemView) {
            super(itemView);
            tradeAmount = itemView.findViewById(R.id.trade_amount);
            tradeTime = itemView.findViewById(R.id.trade_time);
            divideAmount = itemView.findViewById(R.id.divide_amount);
            tradeType = itemView.findViewById(R.id.trade_type);
        }
    }

}
